from fastapi import FastAPI
from pydantic import BaseModel
import jpype

app = FastAPI()


class Body(BaseModel):
    name: str
    age: int


@app.get("/")
async def root():
    return {"message": "Hello World"}


@app.get("/hello/{name}")
async def say_hello(name: str):
    if not jpype.isJVMStarted():
        jvmPath = jpype.getDefaultJVMPath()
        jpype.startJVM(jvmPath, "-ea", r"-Djava.class.path={}".format(
            "C:\\Users\\nidaye\IdeaProjects\pyDemo\\target\pyDemo-1.0-SNAPSHOT.jar"))
    Demo1 = jpype.JClass("example.d1.Demo1")
    instance = Demo1()
    hello = instance.getHello(name)
    return {"message": f"handle {hello}"}

@app.post("/post")
def post_demo(b: Body):
    return b
